#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef int(*callback_t)(void*, void*);

int comp (void* para1, void* para2, callback_t f)
{
    return f (para1, para2);
}

int g_qsort (void* base, void* last, const int t_size, callback_t real_comp)
{
    void* left;
    void* right;
    void* key;
    switch (t_size)
    {
        case 4 : {typedef T int;break;}
        //{left = (int*)base;right = (int*)last;break; }
    }
    //void* key = malloc(t_size);
    //memcpy (key, base, t_size);
    T left = *((T*)base);
    while ( left < right )
    {
        while ( left < right && comp(key, right, real_comp) < 0)
        {
        //printf ("line is %d \n", __LINE__);
                right--;
        }
        if ( left < right)
        {
            memcpy (left, right, t_size);
            left++;
        }
        //printf ("%d \n", left - base);
        //printf ("%d \n", last - right);
        while ( left < right && comp(key, left, real_comp) > 0 )
        {
        //printf ("line is %d \n", __LINE__);
                left++;
        }
        if (left < right)
        {
            memcpy (right, left, t_size);
            //left += t_size;
            right--;
        }
    }
    memcpy (left, key, t_size);
    free (key);
}

int comp_int (const int a, const int b)
{
    return a - b;
}

int main ()
{
    int n[5] = {12,34,5,56,7};
    g_qsort (n, (n+4), sizeof(int), (void*)comp_int);
    int i;
    for (i = 0; i < 5; i++)
    {
        printf ("%d ", n[i]);
    }
    printf ("\n");
    return 0;
}

